var url = "http://" + buildURL() + "/";

$(function() {
	
	if($(window).width()<1224){
		$('#rNav').hide();
		$('#droppable').css('width','950px');
	}else{
		$('#rNav').show();
		$('#droppable').css('width',$(window).width()-320);
		$('#divOnline').css('height',$(window).height()-320);	
	}
	
	//$('#droppable').css('max-width','960px');
	$(window).resize(function(){
		if($(window).width()<1224){
			$('#rNav').hide();
			$('#droppable').css('width','960px');
		}else{
			$('#rNav').show();
			$('#droppable').css('width',$(window).width()-320);
			$('#divOnline').css('height',$(window).height()-320);	
		}
	});
	
	$(window).scroll(function(){
		if($(window).scrollTop() == $(document).height() - $(window).height()){	
			// load more records here
		} 
	});

	getQuickNav();
});


function getTables(){
	$.ajax({
	url: "/tables/",
	cache: false
	}).done(function( html ) {
		$("#divTables").html("");
		$("#divTables").append(html);
		$("#divTables").hide();
		$("#divTables").fadeIn();
		getReports();
	});
}

function getReports(){
	$.ajax({
	url: "/reports/",
	cache: false
	}).done(function( html ) {
		$("#divReports").html("");
		$("#divReports").append(html);
		$("#divReports").hide();
		$("#divReports").fadeIn();
		//getRequests("24","tbl_Cash_Advance","0", "Cash Advance");
		// Announcement Functions
			announcement.postAnnouncement(".btnAnnouncement"); // save to the database;
			announcement.getAnnouncementPost(); // load announcement from the database
			announcement.deleteRecord(); // Delete Announcement
			announcement.addComment(".addButtonComment");
			announcement.getCommentPost();
        // Showing reply box
        announcement.showReply();
	});
}

function getRequests(tableID, tblname, requestid, formName){

	$.ajax({
	url: "/requestJSON/" + tblname + "/" + requestid ,
	cache: false
	}).done(function( results ) {
		var users=["Stephen Cury","Harrison Barnes", "Kent Bazemore", "Andris Biedrins", "Andrew Bogut", "Festus Ezeli", "Draymond Green"];
		
		var output = '<html><head></head><body  style="background-color:#fafafa"><h4>Home / Forms /</h4><h2><img src="/images/Posts.png" style="vertical-align:middle" style="border:0px">&nbsp;' + formName +'</h2><table cellpadding=0 cellspacing=0 width="98%">';
		output += '<tr style="background-color:#f4f4f4">';
		output += '<td colspan=2><a href="form/' + tableID + '/0/" class="bluebutton">New Request</a></td>';
		output += '<td align="right"><input type="text" value="Search..." style="width:200px;font-style:Italic;color:#777" /></td>';
		output += '</tr>';			
		output += '<tr>';
		output += '<td colspan=3><hr></td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3>';
			
		for(var index in results){
			output += '<div class="requestview">';
			output += '<div style="float:left;padding-right:10px">';
			output += '<img class="pic" src="/images/' +  results[index].ID +'.jpg" style="vertical-align:middle">';
			output += '</div>';
			output += '<div class="requestBox">';
				
			if(results[index].Unread=="1"){
				output += '<img src="/images/ui/new_mail.png" style="vertical-align:middle">&nbsp;';
			}else{
				output += '<img src="/images/ui/mail-open.png" style="vertical-align:middle">&nbsp;';
			}
			output += '<h4 style="display: inline;">'+ users[index] +'</h4><br/><br/>';
			output += '<font color="#777" style="font-weight:normal"> submitted a request with Tracking Number: </font>'	
			output += '<a href="form/' + results[index].WS_ID + '/' + results[index].ID + '/" class="editDelete">' + results[index].TrackNo + '</a>';
			output += '<ul style=" padding: 0;list-style-type: none;">';
			output += '<li style="float: left"><a href="form/' + results[index].WS_ID + '/' + results[index].ID + '/" class="editDelete">View</a></li>';
			output += '<li style="float: left"><a href="form/' + results[index].WS_ID + '/' + results[index].ID + '/" class="editDelete">Approve</a></li>';
			output += '<li style="float: left"><a href="form/' + results[index].WS_ID + '/' + results[index].ID + '/" class="editDelete">Reject</a></li>';
			output += '<li style="float: left"><a href="form/' + results[index].WS_ID + '/' + results[index].ID + '/" class="editDelete">Comment</a></li>';
			output += '<li style="float: left"><font color="#777"><abbr class="timeago" title="' + results[index].DateCreated + '"> '+ results[index].DateCreated +'</abbr></li>';
			output += '</ul>';
			output += '</div><br style="clear:both;"/>';
			output += '</div><br/>';
		}
	
		if(results.length==0){
			output += '<div class="notification_warning">';
			output +=  'No record found.';
			output +=  '</div>';
		}
		
		output += '</td>';
		output += '</tr>';
		output += '</table>';
		output += '</body></html>';
		
		$("#divView").html("");
		$("#divView").append(output);	
		$("#divView").hide();
		$("#divView").fadeIn();
		jQuery("abbr.timeago").timeago();
	});
}

function getQuickNav(){
	$.ajax({
	url: "/quicknav/",
	cache: false
	}).done(function( html ) {
		$("#divQuickNav").append(html);
		$("#divQuickNav").hide();
		$("#divQuickNav").fadeIn();
		$("#divQuickNav").jScrollPane({
			horizontalGutter:5,
			verticalGutter:5,
			'showArrows': false
		});
		$('.jspDrag').hide();
		$('.jspScrollable').mouseenter(function(){
			$(this).find('.jspDrag').stop(true, true).fadeIn('slow');
		});
		$('.jspScrollable').mouseleave(function(){
			$(this).find('.jspDrag').stop(true, true).fadeOut('slow');
		});
		getOnline();
	});
}

function getOnline(){
	$.ajax({
	url: "/userJSON/0",
	cache: false
	}).done(function( results ) {
			var onclick ="alert('For premium account only.')"
			var output = '<table border=0 cellpadding=2 cellspacing=0 width="100%">';
			
			for (var index in results) {
				output += '<tr class="rightNavRow" onclick="'+ onclick +'">';
				output += '<td width="20px"><img src="/images/users/' + results[index].id +'.jpg" width="30px" height="30px"></td>';
				output += '<td>' + results[index].Display_Name + '</td>';
				output += '<td align="center"><img src="/images/online.png"></td>';
				output += '</tr>';
			}
			
			output += '</table>';
			
			//lower right
			$("#divOnlineLowerRight").append(output);
			$("#divOnlineLowerRight").hide();
			$("#divOnlineLowerRight").fadeIn();
			$("#divOnlineLowerRight").jScrollPane({
				horizontalGutter:5,
				verticalGutter:5,
				'showArrows': false
			});
			
			//dock on right
			$("#divOnline").append(output);
			$("#divOnline").hide();
			$("#divOnline").fadeIn();
			$("#divOnline").jScrollPane({
				horizontalGutter:5,
				verticalGutter:5,
				'showArrows': false
			});
		/*	
		$( "#lrNav" ).accordion({
			collapsible: true,
			active: false
		});
		*/
		
		$('#lrNav h4').click(function(e) {
				//Close all <div> but the <div> right after the clicked <a>
				$(e.target).next('div').siblings('div').slideUp();
				//Toggle open/close on the <div> after the <a>, opening it if not open.
				$(e.target).next('div').slideToggle();
		});
		
		$('.ui-accordion-header').css('style','background:#444; border:0');
		
		$('.jspDrag').hide();
		$('.jspScrollable').mouseenter(function(){
			$(this).find('.jspDrag').stop(true, true).fadeIn('slow');
		});
		$('.jspScrollable').mouseleave(function(){
			$(this).find('.jspDrag').stop(true, true).fadeOut('slow');
		});
		getTables();
	});
}




function initTabs(){
	$("#content div").hide(); // Initially hide all content
    $("#tabs li:first").attr("id","current"); // Activate first tab
    $("#content div:first").fadeIn(); // Show first tab content
    
    $('#tabs a').click(function(e) {
        e.preventDefault();
        if ($(this).closest("li").attr("id") == "current"){ //detection for current tab
         return       
        }
        else{             
        $("#content div").hide(); //Hide all content
        $("#tabs li").attr("id",""); //Reset id's
        $(this).parent().attr("id","current"); // Activate this
        $('#' + $(this).attr('name')).fadeIn(); // Show content for current tab
        }
    });
	
}